<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\Store;
use App\Models\Report;

class SellCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sell:count';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = Carbon::yesterday()->timestamp;

        $end = Carbon::today()->timestamp;

        $res = Store::where("fee_type",2)->get();


        $data = $res->map(function($v){
            $re = [];
            $re['store_name'] = $v->store_name;
            $re['type_name'] = $v->type == 1?'储时':'储值';
            $re['duration'] = $v->type == 1?$v->duration:'--';
            $re['money'] = $v->type == 2?$v->money/100:'--';
            $re['settle_period'] = $v->settle_period.'月';
            $re['settle_time'] = $v->settle_time;
            $re['phone'] = $v->phone;
            $re['address'] = $v->address;
            $re['note'] = $v->note;
            return $re;
        });
        //存入数据库
        $report = new Report();
        $report->report_date = Carbon::yesterday()->format('Y-m');
        $report->type = 'sell_count_month_report';
        $report->value = $data;
        $report->save();

        $this->info('预售下发统计');
    }


}
